<?php

namespace XWAM\Addon\PXCallSystem\Model;

/**
 * 站点配置数据库模型
 *
 * @author SoChishun <14507247@qq.com>
 * @version 1.0
 * @since 1.0.0 <2015-3-27> SoChishun Added.
 */
class SiteConfigModel extends AppbaseModel {

    /**
     * 数据表名称
     * @var string
     */
    protected $tableName = 'tsite_config';

    /**
     * 字段定义
     * @var array
     */
    protected $fields = array('id', 'user_id', 'site_name', 'category_id', 'logo_url', 'home_page', 'icp_license_code', 'contact_name', 'contact_telphone', 'contact_mobile', 'contact_address', 'company_name', 'about_us', 'contact_us', 'closed_announcement', 'ordinal', 'ex_tags', 'state', 'create_time', 'update_time', 'site_id', '_pk' => 'id', '_autoinc' => true);

    /**
     * 命名范围
     * @var array
     */
    protected $_scope = array();

    /**
     * 是否批量验证
     * @var boolean
     */
    protected $patchValidate = true;

    /**
     * 验证规则
     * @var array
     */
    protected $_validate = array();

    /**
     * 自动填充
     * @var array
     */
    protected $_auto = array();

    /**
     * 获取系统站点配置数据
     * @param boolean $refresh 是否强制刷新
     * @return array|false
     * @since 1.0 <2015-3-27> SoChishun Added.
     */
    public function get_sys_site($refresh = false) {
        $key = 'SiteConfig/site_0';
        return $this->get_config_cache($key, array('user_id' => 0, 'ex_tags' => 'SITE'), $refresh);
    }

    public function get_shop_config($user_id, $refresh = false) {
        $key = 'ShopConfig/shop_' . $user_id;
        return $this->get_config_cache($key, array('user_id' => $user_id, 'ex_tags' => 'SHOP'), $refresh);
    }

    /**
     * 保存站点配置文件
     * @return type
     * @since 1.0 <2015-3-27> SoChishun Added.
     */
    public function save_sys_site() {
        $rule = array(
            array('site_name', 'require', '站点名称无效'),
            array('state', 'number', '状态无效'),
        );
        if (!$this->validate($rule)->create()) {
            return $this->returnMsg(false, $this->getError());
        }
        // 上传文件
        $files = sofn_upload(array('skipEmpty' => true,'savePath'=>'Logo/'));
        if (is_array($files)) {
            $this->logo_url = $files['logo_url']['filepath'];
        }
        if ($this->data['id']) {
            $result = $this->save();
        } else {
            $this->user_id = 0;
            $this->ex_tags = 'SITE';
            $result = $this->add();
        }
        if(false!==$result){
            $this->get_sys_site(true); // 保存成功后刷新缓存
        }
        return $this->returnMsg($result);
    }

    /**
     * 返回配置数据或刷新缓存
     * 
     * @param string $key 缓存名称
     * @param array|string $where 查询条件
     * @param boolean $refresh 是否强制刷新数据
     * @return false|array
     * @since 1.0 <2015-3-27> SoChishun Added.
     */
    protected function get_config_cache($key, $where, $refresh = false) {
        if (!$refresh) {
            $data = F($key);
            if ($data) {
                return $data;
            }
        }
        $data = $this->where($where)->find();
        F($key, $data);
        return $data;
    }

}
